package com.tz.farmer.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.mapper.ShareRecordMapper;
import com.tz.farmer.model.ShareRecord;
import com.tz.farmer.service.WebShareTheRecordService;
import com.tz.farmer.so.ShareRecordSo;
import com.tz.farmer.util.BeanConvertUtils;
import com.tz.farmer.util.ResultExM;
import com.tz.farmer.vo.ShareRecordVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Service
public class WebShareTheRecordServiceImpl extends ServiceImpl<ShareRecordMapper , ShareRecord> implements WebShareTheRecordService {

    @Resource
    ShareRecordMapper shareRecordMapper;


    @Override
    public ResultEx getShareTheRecordListConditions(ShareRecordSo so) {
        if(ObjectUtils.isEmpty(so)
                || "".equals(so.getFlag())
                || "".equals(so.getCurrentPage())
                || so.getCurrentPage()==null
                || "".equals(so.getPageSize())
                || so.getPageSize() == null){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        /*IPage<ShareRecord> page = shareRecordMapper.selectPage(
                new Page<ShareRecord>(so.getCurrentPage(), so.getPageSize()),
                new QueryWrapper<ShareRecord>()
                        .eq("property", so.getFlag())
                        .like(StringUtils.isNotEmpty(so.getKeyword()), "classify", so.getKeyword())
                        .like(StringUtils.isNotEmpty(so.getKeyword()), "p_name", so.getKeyword())
                        .last(StringUtils.isNotEmpty(so.getStartTime()),
                                " and 1= 1 and share_time >= STR_TO_DATE( '"+so.getStartTime()+" 00:00:00','%Y-%m-%d %H:%i:%s')  and  share_time <= STR_TO_DATE(' "+so.getEndTime()+ " 23:59:59','%Y-%m-%d %H:%i:%s')" )
                        .last( "   order by share_time DESC "));*/
        int total = shareRecordMapper.countFlag(so.getFlag(), so.getKeyword(), so.getStartTime(), so.getEndTime());
        int n = (so.getCurrentPage()-1)*so.getPageSize();
        int m =so.getPageSize();
        List<ShareRecord> list = shareRecordMapper.selectConditionList(so.getFlag(), so.getKeyword(), so.getStartTime(), so.getEndTime(),n,m);
        Map<String, Object> map = new Hashtable<>();
        map.put("total",total);
        map.put("data",list);
        return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, map);
    }

    @Override
    public List<ShareRecordVo> getShareTheRecordDownLoadList(ShareRecordSo so) {
        List<ShareRecordVo> listVo = new ArrayList<>();
        List<ShareRecord> list = shareRecordMapper.selectDownLoadList(so.getFlag(), so.getKeyword(), so.getStartTime(), so.getEndTime(),so.getIds());

        if(CollectionUtils.isNotEmpty(list) || list.size() > 0){
            for ( int i = 0 ; i<list.size() ; i++) {
                ShareRecordVo shareRecordVo = BeanConvertUtils.copyProperties(list.get(i), ShareRecordVo.class);
                LocalDateTime createTime = list.get(i).getShareTime();
                DateTimeFormatter sf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                shareRecordVo.setShareTime(sf.format(createTime));
                shareRecordVo.setProductName(list.get(i).getPName());
                shareRecordVo.setMerchantName(list.get(i).getMName());
                listVo.add(shareRecordVo);
            }
        }
        if(CollectionUtils.isEmpty(listVo) || listVo.size() < 0){
            return new ArrayList<>();
        }
        return  listVo;
    }

}
